﻿using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media.Media3D;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;

namespace RotovapPlatform.UI.ViewModels.MaterialManagement
{
	/// <summary>
	/// 物料查询视图模型
	/// </summary>
	public partial class MaterialQueryViewModel : ObservableObject
	{
		private readonly ILogger<MaterialQueryViewModel> _logger;

		/// <summary>
		/// 物料列表
		/// </summary>
		[ObservableProperty]
		private ObservableCollection<MaterialInfo> _materials = new ObservableCollection<MaterialInfo>();

		/// <summary>
		/// 选中的物料
		/// </summary>
		[ObservableProperty]
		private MaterialInfo _selectedMaterial;

		/// <summary>
		/// 物料类型列表
		/// </summary>
		[ObservableProperty]
		private ObservableCollection<string> _materialTypes = new ObservableCollection<string>();

		/// <summary>
		/// 选中的物料类型
		/// </summary>
		[ObservableProperty]
		private string _selectedMaterialType;

		/// <summary>
		/// 物料名称筛选
		/// </summary>
		[ObservableProperty]
		private string _nameFilter = string.Empty;

		/// <summary>
		/// 物料编号筛选
		/// </summary>
		[ObservableProperty]
		private string _codeFilter = string.Empty;



		/// <summary>
		/// 物料详情是否可见
		/// </summary>
		[ObservableProperty]
		private bool _isDetailVisible;

		///// <summary>
		///// 构造函数
		///// </summary>
		///// <param name="logger">日志记录器</param>
		//public MaterialQueryViewModel(ILogger<MaterialQueryViewModel> logger)
		//{
		//	_logger = logger;
		//	_logger.LogInformation("物料查询视图模型已初始化");

		//	// 初始化命令
		//	SearchCommand = new RelayCommand(async () => await SearchMaterialsAsync());
		//	ExportCommand = new RelayCommand(ExportMaterials, CanExportMaterials);
		//	ViewDetailCommand = new RelayCommand<MaterialInfo>(ViewMaterialDetail);
		//	ClearFiltersCommand = new RelayCommand(ClearFilters);
		//}

		/// <summary>
		/// 搜索命令
		/// </summary>
		public ICommand SearchCommand { get; }

		/// <summary>
		/// 导出命令
		/// </summary>
		public ICommand ExportCommand { get; }

		/// <summary>
		/// 查看详情命令
		/// </summary>
		public ICommand ViewDetailCommand { get; }

		/// <summary>
		/// 清除筛选条件命令
		/// </summary>
		public ICommand ClearFiltersCommand { get; }

		/// <summary>
		/// 初始化
		/// </summary>
		//	public async Task InitializeAsync()
		//	{
		//		try
		//		{
		//			IsLoading = true;
		//			_logger.LogInformation("正在初始化物料查询数据...");

		//			// 初始化物料类型
		//			MaterialTypes.Clear();
		//			MaterialTypes.Add("全部");
		//			MaterialTypes.Add("原料");
		//			MaterialTypes.Add("中间品");
		//			MaterialTypes.Add("成品");
		//			MaterialTypes.Add("辅料");
		//			MaterialTypes.Add("包装材料");
		//			MaterialTypes.Add("其他");

		//			SelectedMaterialType = "全部";

		//			// 这里将来会添加实际的数据加载逻辑
		//			await Task.Delay(500); // 模拟加载延迟

		//			// 模拟添加一些物料数据
		//			await SearchMaterialsAsync();

		//			_logger.LogInformation("物料查询数据初始化完成");
		//		}
		//		catch (Exception ex)
		//		{
		//			_logger.LogError(ex, "初始化物料查询数据时发生错误");
		//		}
		//		finally
		//		{
		//			IsLoading = false;
		//		}
		//	}

		//	/// <summary>
		//	/// 搜索物料
		//	/// </summary>
		//	private async Task SearchMaterialsAsync()
		//	{
		//		try
		//		{
		//			IsLoading = true;
		//			_logger.LogInformation("正在搜索物料...");

		//			// 这里将来会添加实际的搜索逻辑
		//			await Task.Delay(500); // 模拟搜索延迟

		//			// 清空现有数据
		//			Materials.Clear();

		//			// 模拟添加一些物料数据
		//			var random = new Random();
		//			var materialTypes = new[] { "原料", "中间品", "成品", "辅料", "包装材料", "其他" };
		//			var units = new[] { "kg", "g", "L", "mL", "个", "箱" };
		//			var suppliers = new[] { "供应商A", "供应商B", "供应商C", "供应商D", "供应商E" };
		//			var storageConditions = new[] { "常温", "冷藏", "冷冻", "避光", "干燥" };

		//			for (int i = 0; i < 20; i++)
		//			{
		//				var materialType = materialTypes[random.Next(materialTypes.Length)];
		//				var materialCode = $"M{random.Next(1000, 10000)}";
		//				var materialName = $"物料-{i + 1:D2}";

		//				// 如果选择了特定物料类型且不是"全部"，则只添加该类型的物料
		//				if (SelectedMaterialType != "全部" && materialType != SelectedMaterialType)
		//				{
		//					continue;
		//				}

		//				// 如果有名称筛选且不匹配，则跳过
		//				if (!string.IsNullOrEmpty(NameFilter) && !materialName.Contains(NameFilter))
		//				{
		//					continue;
		//				}

		//				// 如果有编号筛选且不匹配，则跳过
		//				if (!string.IsNullOrEmpty(CodeFilter) && !materialCode.Contains(CodeFilter))
		//				{
		//					continue;
		//				}

		//				Materials.Add(new MaterialInfo
		//				{
		//					Id = i + 1,
		//					Code = materialCode,
		//					Name = materialName,
		//					Type = materialType,
		//					Specification = $"{random.Next(1, 100)} {units[random.Next(units.Length)]}",
		//					Quantity = random.Next(1, 1000),
		//					Unit = units[random.Next(units.Length)],
		//					Supplier = suppliers[random.Next(suppliers.Length)],
		//					ManufactureDate = DateTime.Now.AddDays(-random.Next(1, 365)),
		//					ExpiryDate = DateTime.Now.AddDays(random.Next(1, 365)),
		//					BatchNumber = $"B{random.Next(10000, 100000)}",
		//					StorageCondition = storageConditions[random.Next(storageConditions.Length)],
		//					StorageLocation = $"区域{random.Next(1, 5)}-货架{random.Next(1, 10)}-位置{random.Next(1, 20)}",
		//					Description = $"这是物料{i + 1}的描述信息，包含了该物料的基本特性和用途。"
		//				});
		//			}

		//			_logger.LogInformation($"搜索到 {Materials.Count} 个物料");
		//		}
		//		catch (Exception ex)
		//		{
		//			_logger.LogError(ex, "搜索物料时发生错误");
		//		}
		//		finally
		//		{
		//			IsLoading = false;
		//		}
		//	}

		//	/// <summary>
		//	/// 导出物料
		//	/// </summary>
		//	private void ExportMaterials()
		//	{
		//		try
		//		{
		//			_logger.LogInformation("正在导出物料数据...");

		//			// 这里将来会添加实际的导出逻辑

		//			_logger.LogInformation("物料数据导出完成");
		//		}
		//		catch (Exception ex)
		//		{
		//			_logger.LogError(ex, "导出物料数据时发生错误");
		//		}
		//	}

		//	/// <summary>
		//	/// 是否可以导出物料
		//	/// </summary>
		//	private bool CanExportMaterials()
		//	{
		//		return Materials.Count > 0;
		//	}

		//	/// <summary>
		//	/// 查看物料详情
		//	/// </summary>
		//	private void ViewMaterialDetail(MaterialInfo material)
		//	{
		//		if (material == null)
		//		{
		//			return;
		//		}

		//		SelectedMaterial = material;
		//		IsDetailVisible = true;
		//	}

		//	/// <summary>
		//	/// 清除筛选条件
		//	/// </summary>
		//	private void ClearFilters()
		//	{
		//		NameFilter = string.Empty;
		//		CodeFilter = string.Empty;
		//		SelectedMaterialType = "全部";
		//	}
		//}

		/// <summary>
		/// 物料信息类
		/// </summary>
		public class MaterialInfo
		{
			/// <summary>
			/// 物料ID
			/// </summary>
			public int Id { get; set; }

			/// <summary>
			/// 物料编号
			/// </summary>
			public string Code { get; set; }

			/// <summary>
			/// 物料名称
			/// </summary>
			public string Name { get; set; }

			/// <summary>
			/// 物料类型
			/// </summary>
			public string Type { get; set; }

			/// <summary>
			/// 规格
			/// </summary>
			public string Specification { get; set; }

			/// <summary>
			/// 数量
			/// </summary>
			public double Quantity { get; set; }

			/// <summary>
			/// 单位
			/// </summary>
			public string Unit { get; set; }

			/// <summary>
			/// 供应商
			/// </summary>
			public string Supplier { get; set; }

			/// <summary>
			/// 生产日期
			/// </summary>
			public DateTime ManufactureDate { get; set; }

			/// <summary>
			/// 有效期
			/// </summary>
			public DateTime ExpiryDate { get; set; }

			/// <summary>
			/// 批号
			/// </summary>
			public string BatchNumber { get; set; }

			/// <summary>
			/// 存储条件
			/// </summary>
			public string StorageCondition { get; set; }

			/// <summary>
			/// 存储位置
			/// </summary>
			public string StorageLocation { get; set; }

			/// <summary>
			/// 描述
			/// </summary>
			public string Description { get; set; }
		}
	}
}